Decomposing scRNA-seq data using NMF - a demo

Non-negative matrix factorization is a tool for the analysis of high dimensional data that allows extracting sparse and meaningful features from a set of non-negative data vectors. It is well suited for decomposing scRNA-seq data, effectively reducing large complex matrices (\(10^4\) of genes times \(10^5\) of cells) into a few interpretable gene programs. It has been especially used to extract recurrent gene programs in cancer cells (see e.g. Barkely et al. (2022) and Gavish et al. (2023)), which are otherwise difficult to integrated and analyse jointly.

Here, to illustrate the methods implemented in the GeneNMF package, we will apply NMF on a single-cell cell dataset of human PBMCs - a downsampled version of the dataset published by Hao et al. (2021).

Set up the enviroment

Here are some packages you’ll need for this demo:

library(remotes)
remotes::install_github("carmonalab/GeneNMF")
library(GeneNMF)

library(Seurat)
library(ggplot2)
library(UCell)
library(patchwork)
library(Matrix)
library(RcppML)
library(ggtree)
library(tidytree)
library(viridis)

Then download the test dataset for this demo.

ddir <- "input"
data.path <- sprintf("%s/pbmc_multimodal.downsampled20k.seurat.rds", ddir)

if (!file.exists(data.path)) {
    dir.create(ddir)
    dataUrl <- "https://www.dropbox.com/s/akzu3hp4uz2mpkv/pbmc_multimodal.downsampled20k.seurat.rds?dl=1"
    download.file(dataUrl, data.path)
}

seu <- readRDS(data.path)

NMF for dimensionality reduction

NMF can be applied to reduce the dimensionality of the data from tens of thousand of genes to a few dimensions (similarly to PCA). With the RunNMF() function, it can be directly applied on a Seurat object, and it will save the NMF results as a new dimensionality reduction.

ndim <- 15

seu <- FindVariableFeatures(seu, nfeatures = 1000)
seu <- runNMF(seu, k = ndim, assay="SCT")
seu@reductions$NMF
## A dimensional reduction object with key NMF_ 
##  Number of dimensions: 15 
##  Number of cells: 20000 
##  Projected dimensional reduction calculated:  FALSE 
##  Jackstraw run: FALSE 
##  Computed using assay: SCT

We can also further reduced the dimensionality to 2 dimensions using UMAP; in this space we can visualize all cells in a single plot.

seu <- RunUMAP(seu, reduction = "NMF", dims=1:ndim, reduction.name = "NMF_UMAP", reduction.key = "nmfUMAP_")
DimPlot(seu, reduction = "NMF_UMAP", group.by = "celltype.l1", label=T) + theme(aspect.ratio = 1,
                                                            axis.text = element_blank(),
    axis.title = element_blank(),
    axis.ticks = element_blank()) + ggtitle("NMF UMAP") + NoLegend()

Consistent NMF programs across multiple samples

Identification of robust gene programs requires their detection across samples and variability of input parameters. Perhaps the most crucial parameter to NMF is the dimensionality k, which corresponds to the number of programs of the low-dimensional matrix. To determine robust programs, we can run NMF over multiple numbers of k and determine programs that are consistenly found across these runs. The multiNMF() function automatically performs NMF over a list of samples and for multiple values of k:

seu.list <- SplitObject(seu, split.by = "donor")

geneNMF.programs <- multiNMF(seu.list, assay="SCT", slot="data", k=4:9, L1=c(0,0),
                    do_centering=TRUE, nfeatures = 2000)

We can now combine the gene programs identified over multiple samples and numbers of k into metaprograms (MPs), i.e. consensus programs that are robustly identified across NMF runs. Here we will define 10 MPs:

geneNMF.metaprograms <- getMetaPrograms(geneNMF.programs,
                                        nprograms=10,
                                        max.genes=50,
                                        hclust.method="ward.D2",
                                        min.confidence=0.3)

It can be useful to visualize pairwise similarity (Jaccard Index) between individual gene programs that compose meta-programs. We can see “blocks” corresponding to gene programs of high similarity across datasets and values of k. We can then cut the similarity tree at a given height to find blocks of similar programs and derive consensus gene signatures for each block. For example, here we cut the tree to the height corresponding to 10 clusters of programs (i.e. 10 MPs):

ph <- plotMetaPrograms(geneNMF.metaprograms, jaccard.cutoff = c(0,0.8))
ph$heatmap

ph$tree

We can also inspect useful statistics, such as the “sample coverage” (in what fraction of samples the MP was detected); or the silhoette coefficient (how similar are individual programs in a MP relative to programs in other MPs - the higher the better).

Based on these metrics, one may decide to drop some of the programs, e.g. if they are specific for few samples only (low sample coverge), or have bad internal consistency (low silhouette and meanJaccard).

geneNMF.metaprograms$metaprograms.metrics
##      sampleCoverage  silhouette meanJaccard numberGenes
## MP1           1.000  0.82605598       0.834          50
## MP2           1.000  0.58203151       0.605          50
## MP3           1.000  0.56295385       0.579          50
## MP4           1.000  0.51359676       0.541          50
## MP5           1.000  0.42393323       0.550          50
## MP6           1.000  0.33653086       0.361          49
## MP7           1.000  0.26344233       0.321          50
## MP8           1.000  0.23339237       0.404          50
## MP9           1.000 -0.01466146       0.136          33
## MP10          0.875  0.28620891       0.340          50

What are the genes driving each program?

t(as.data.frame(lapply(geneNMF.metaprograms$metaprograms.genes, head)))
##      [,1]      [,2]       [,3]       [,4]      [,5]     [,6]       
## MP1  "ACRBP"   "ARHGAP18" "BEX3"     "CAVIN2"  "CD9"    "CLU"      
## MP2  "AKR1C3"  "CD160"    "CD247"    "HOPX"    "KLRF1"  "LAIR2"    
## MP3  "BANK1"   "CD22"     "CD79A"    "IGHD"    "IGHG3"  "IGHM"     
## MP4  "ADTRP"   "CCR7"     "CD27"     "FHIT"    "LEF1"   "LINC00861"
## MP5  "CD14"    "CXCL8"    "CYP1B1"   "EGR1"    "FCN1"   "FOS"      
## MP6  "C1orf54" "CLEC10A"  "CACNA2D3" "PID1"    "RTN1"   "ZNF503"   
## MP7  "CDKN1C"  "MS4A7"    "HES4"     "HMOX1"   "SMIM25" "C1QA"     
## MP8  "CYP1B1"  "RBP7"     "S100A12"  "S100A8"  "S100A9" "VCAN"     
## MP9  "IER3"    "SGK1"     "IL1RN"    "CCL3"    "CCL3L1" "CCL4L2"   
## MP10 "CFH"     "KLRG1"    "TRGC2"    "CAMK2N1" "LAG3"   "LINC01871"

Intepretation of gene programs by GSEA

To aid the interpretation of gene programs, we can compare them to known signatures from public databases. The runGSEA() function can be useful to scan msigDB and evaluate the overlap of detected gene programs with signatures in the databases. Here we compare to the “C8” category (cell type signature gene sets); but other classes such as “H” (hallmark gene sets) may be more relevant in other contexts.

library(msigdbr)
library(fgsea)
top_p <- lapply(geneNMF.metaprograms$metaprograms.genes, function(program) {
  runGSEA(program, universe=rownames(seu), category = "C8")
})

For example, Program 1 appears to correlate significantly with Platelet/Megakaryocyte signatures:

head(top_p$MP9)
##                                                    pathway         pval
##                                                     <char>        <num>
## 1: DURANTE_ADULT_OLFACTORY_NEUROEPITHELIUM_DENDRITIC_CELLS 7.040370e-33
## 2:            TRAVAGLINI_LUNG_OLR1_CLASSICAL_MONOCYTE_CELL 1.710838e-30
## 3:            GAO_LARGE_INTESTINE_24W_C11_PANETH_LIKE_CELL 7.470610e-23
## 4:                     TRAVAGLINI_LUNG_EREG_DENDRITIC_CELL 8.009332e-22
## 5:                          MANNO_MIDBRAIN_NEUROTYPES_HMGL 2.099306e-20
## 6:      DURANTE_ADULT_OLFACTORY_NEUROEPITHELIUM_MAST_CELLS 1.547482e-17
##            padj overlap  size overlapGenes
##           <num>   <int> <int>       <list>
## 1: 4.914178e-30      18   116 ATP2B1-A....
## 2: 5.970824e-28      25   711 ACSL1, A....
## 3: 1.738162e-20      17   315 BCL2A1, ....
## 4: 1.397628e-19      19   565 ACSL1, A....
## 5: 2.930631e-18      18   547 BCL2A1, ....
## 6: 1.800237e-15      10    74 CCL4, CD....

Signature scores for gene programs

A simple way to evaluate gene programs learned from the data is to calculate gene signature scores with the UCell package.

mp.genes <- geneNMF.metaprograms$metaprograms.genes
seu <- AddModuleScore_UCell(seu, features = mp.genes, assay="SCT", ncores=4, name = "")

We can see how many of the programs are enriched in specific cell subtypes (cell type annotation from the original study).

VlnPlot(seu, features=names(mp.genes), group.by = "celltype.l1",
        pt.size = 0, ncol=5)

Signature scores to define integrated space

Individual cells can now be represented in terms of their gene program scores. Importantly, here the gene programs were learned as a consensus of gene programs found across multiple samples – as opposed to calculating NMF once on the whole dataset. This can be an effective strategy to mitigate batch effects, as meta-programs (MPs) are a consensus of gene programs consistently found across individual samples. Let’s store these coordinates in the Seurat object:

matrix <- seu@meta.data[,names(mp.genes)]
dimred <- scale(matrix)

colnames(dimred) <- paste0("MP_",seq(1, ncol(dimred)))
#New dim reduction
seu@reductions[["MPsignatures"]] <- new("DimReduc",
                                         cell.embeddings = dimred,
                                         assay.used = "RNA",
                                         key = "MP_",
                                         global = FALSE)

We can also use these scores to generate a UMAP representation and visualize the data in 2D:

set.seed(123)
seu <- RunUMAP(seu, reduction="MPsignatures", dims=1:length(seu@reductions[["MPsignatures"]]),
               metric = "euclidean", reduction.name = "umap_MP")

How do the signature scores for the meta-programs look like in the combined space?

FeaturePlot(seu, features = names(mp.genes), reduction = "umap_MP", ncol=4) &
  scale_color_viridis(option="B") &
   theme(aspect.ratio = 1, axis.text=element_blank(), axis.ticks=element_blank())

Final remarks

NMF can be a powerful tool to extract gene programs for scRNA-seq data in an unbiased manner. Because it is calculated for each sample separately, it bypasses the need to perform batch effect correction to analyse samples jointly. This aspect makes it particularly interesting for the analysis of gene programs in cancer cells (see e.g. Barkely et al. (2022) and Gavish et al. (2023).

References

  • GeneNMF package - LINK
  • We use the ultra-fast RcppML method for NMF - LINK